let novelChapterApp = function(){
    let currentCatalogs = [];
    let dbName = sessionStorage.getItem("url-db-name");
    let novelID = sessionStorage.getItem("novel-id");
    // let chapterID = 0;//当前章节ID localStorage.getItem("chapter-id");
    let chapterIDMax = 100;//最大章节ID，默认是100
    return {
        init:function(){
            this.initEvent();
            this.getCatalogs();
            this.getChapterContent2();
            this.changeTheme(localStorage.getItem("theme-num"));
        },
        //注册事件
        initEvent:function(){
            let _this = this;
            $("body").keyup(function(obj){
                if(obj.keyCode == 37){//按键左
                    _this.chapterUpDown(-1);
                }else if(obj.keyCode == 39){//按键右
                    _this.chapterUpDown(1);
                }else if(obj.keyCode == 77){//目录
                    _this.toggleCatalog();
                }else if(obj.keyCode == 83){//设置
                    _this.toggleTheme();
                }else if(obj.keyCode == 74){//书架
                    location.href="bookcase.html";
                }
            });
        },
        toggleTheme:function(){
            let obj = $("#panelTheme");
            if(obj.hasClass('hidden')){
                obj.removeClass("hidden");
            }else{
                obj.addClass("hidden");
            }
        },
        //更改主题
        changeTheme:function(flag){
            if(!flag)flag = 1;
            localStorage.setItem("theme-num",flag);
            $(".theme").each(function(i,obj){
                $(obj).css("background","url(../img/theme/theme-"+flag+".png)");
                //更改字体、边框顔色  flag=7 黑色
                if(flag == 7){
                    $(obj).css("color","#666");
                    $(obj).css("border-color","#333");
                }else{
                    $(obj).css("color","#333");
                    $(obj).css("border-color","#d8d8d8");
                }
            })
        },
        //上一章、下一章
        chapterUpDown:function(flag){
            let cID = localStorage.getItem("chapter-id");
            if(flag == -1){
                cID = parseInt(cID) - 1 ;
                if(cID < 0)cID = 0;
            }else{
                cID = parseInt(cID) + 1 ;
                if(cID > chapterIDMax)cID = chapterIDMax;
            }
            localStorage.setItem("chapter-id",cID);
            this.getChapterContent2();
        },
        //获取某章节内容
        getChapterContent:function(obj){
            localStorage.setItem("chapter-id",$(obj).attr("data-chapter-id"));
            this.getChapterContent2();
        },
        //获取某章节内容
        getChapterContent2:function(){
            let _this = this,cID = localStorage.getItem("chapter-id");
            if(!cID){
                cID=0;
                localStorage.setItem("chapter-id","0");
            }
            $.post("/novel/getChapterContent",{
                url_db_name:dbName,
                id:cID
            },function(data){
                data = JSON.parse(data);
                let obj = data.data.data;
                $("#novelChapter").html(obj.content);
                document.getElementsByTagName("html")[0].scrollTop=0;//返回页面顶部
                //如果目录面板打开了，关闭
                obj = $("#novelCatalog0");
                if(!obj.hasClass('hidden')){
                    obj.addClass("hidden");
                }
            });
        },
        changeMenuHeight:function(){
            $("#novelCatalog0").css("height",window.innerHeight+"px");
        },
        //打开、关闭目录
        toggleCatalog:function(){
            this.changeMenuHeight();
            let obj = $("#novelCatalog0");
            if(obj.hasClass('hidden')){
                obj.removeClass("hidden");
            }else{
                obj.addClass("hidden");
            }
        },
        //获取小说目录
        getCatalogs:function(){
            if(currentCatalogs.length > 0){
                this.getCatalogs2(currentCatalogs);
                return;
            }
            if(dbName == '')return;
            let _this = this;
            $.post("/novel/getCatalogs",{
                url_db_name:dbName
            },function(data){
                data = JSON.parse(data);
                let arr = data.data.data;
                currentCatalogs = arr;
                _this.getCatalogs2(arr);
            });
        },
        //将小说目录数据展示在页面上
        getCatalogs2:function(arr){
            let str = "",tmp;
            for(let i=0;i<arr.length;i++){
                tmp = arr[i];
                str += "<li class='list-group-item' onclick='novelChapterApp.getChapterContent(this)'" +
                    " data-chapter-id='"+tmp.id+"'>"+
                    tmp.name+"</li>"
                chapterIDMax = tmp.id;
            }
            $("#novelCatalogs").html(str);
        }
    };
}();
$(document).ready(function(){
    novelChapterApp.init();
});